import Router from '@koa/router'
import { map, pick } from 'lodash'
import {
    insert,
    like,
    select,
    delete as sqlDelete,
    update,
    eq,
} from 'mysql-bricks'
import { query } from '../models'
import dayjs from 'dayjs'

/**
 * 获取套餐列表
 */
export const getPayRecordPageController: Router.Middleware = async (ctx) => {
    // const likePicked = pick(ctx.query, ['name', 'phone'])
    // const likeWhereExp = map(likePicked, (v, k) => like(k, `%${v}%`))
    const eqPicked = pick(ctx.query, ['num', 'type', 'status', 'methodId'])
    const eqWhereExp = map(eqPicked, (v, k) => eq(k, v))
    const page = ctx.query.page || 1
    // 生成sql
    const sql = select()
        .from('pay_record')
        .where(...eqWhereExp)
        .limit(+ctx.query.limit || 10)
        .offset((+page - 1) * 10)
        .orderBy('createdAt DESC')
        .toString()
    console.log(sql)
    // 执行sql
    const res = await query(sql)
    const total = await query('select count(*) as total from pay_record')
    // 返回数据
    ctx.body = {
        total: total[0].total,
        rows: res,
    }
}
/**
 * 获取订单总金额
 */
export const getGenRecordController: Router.Middleware = async (ctx) => {
    const parkId = +ctx.query.parkId
    const parkSpaceType = +ctx.query.parkSpaceType
    const enterTime = ctx.query.enterTime.toString()
    const leaveTime = ctx.query.leaveTime.toString()
    const dishour = dayjs(leaveTime).diff(enterTime, 'hour')//小时差
    const diss = dayjs(leaveTime).diff(enterTime, 's')//秒差
    console.log(diss)
    let dis = dishour
    if (parkSpaceType === 1) {
        ctx.body = {
            price: 0,
            hourPrice: 0,
            dis,
        }
        return
    }
    if (diss%3600 > 0) {//不足一小时
         dis += 1
    }
    const sql =
        'SELECT * FROM park LEFT JOIN price_mode ON price_mode.id = park.`priceModeId` WHERE park.id = ' +
        parkId
    // 执行sql
    const res = await query(sql)
    const realPrice = res[0].realPrice

    // 返回数据
    ctx.body = {
        price: dis * realPrice,
        hourPrice: realPrice,
        dis,
    }
}

/**
 * 添加套餐
 */
export const postPayRecordController: Router.Middleware = async (ctx) => {
    // 获取请求参数
    const data = ctx.request.body
    // 生成sql
    const sql = insert().into('pay_record').values(data).toString()
    // 执行sql
    const res = (await query(sql)) as any
    // 返回数据
    ctx.body = {
        id: res.insertId,
    }
}

/**
 * 修改套餐信息
 */
export const putPayRecordByIdController: Router.Middleware = async (ctx) => {
    const id = +ctx.params.id!
    const data = ctx.request.body
    // 生成sql
    const sql = update('pay_record', data).where('id', id).toString()
    // 执行sql
    const res = await query(sql)
    // 返回数据
    ctx.body = true
}

/**
 * 删除套餐
 */
export const deletePayRecordByIdController: Router.Middleware = async (ctx) => {
    const id = +ctx.params.id!
    // 生成sql
    const sql = sqlDelete().from('pay_record').where('id', id).toString()
    // 执行sql
    const res = await query(sql)
    // 返回数据
    ctx.body = true
}
